import {SUCCESS, getMarket } from '@/api'
import { numberUtils,  toFixed} from '@/utils'
const last24h = {open: '0', high: '0', bottom: '0', close: '0', vol: '0', rfvol: '0', percent: '0'}
const fixed = 8

const state = () => ({
  marketsObj:{},
  marketList:[],
  marketVisibleList:[],
  marketConfig: {},
  btcValues:{}, //币种对BTC的实时价格
  networkSignal: 0,
  market:localStorage.getItem('market') || '', //当前市场
  last24h: last24h,
  events: {},
})

const getters = {
  getMarket(state, getters, rootState){
    let _market = rootState.user.sysParams.defaultMarket && rootState.user.sysParams.defaultMarket.value //获取系统配置参数的默认市场
    _market = state.market || _market
    return state.marketsObj[_market] || state.marketList[0] || {}
  }
}

const mutations = {
  updateMarkets (state, markets) {
    state.marketList = markets
    state.marketVisibleList = markets.filter(item=>{return item.visible})
    state.marketsObj = markets.reduce((obj, item)=>{obj[item.market] = item; return obj}, {})
  },
  updateBtcValues (state, btcValues) {
    state.btcValues = btcValues
  },
  updateMarketConfig (state, marketConfig) {
    state.marketConfig = marketConfig
  },
  updateNetworkSignal (state, signal) {
    state.networkSignal = signal
  },
  updateMarket (state, market) {
    localStorage.setItem('market', market)
    state.market = market
  },
  updateLast24h (state, data) {
    let open = toFixed(data.open, fixed) //开盘价
    let high = toFixed(data.high, fixed) // 24h最高价
    let bottom = toFixed(data.low, fixed) // 24h最低价
    let close = toFixed(data.last, fixed) //收盘价
    let vol = toFixed(data.volume, fixed) // 24h成交量
    let rfvol = toFixed(numberUtils.minus(close, open), fixed) // 涨跌幅
    let percent = !numberUtils.BN(open).equals(0) ? numberUtils.minus(close, open).div(open).mul(100).toFixed(2) : '0.00' //涨跌百分比
    let direction = data.side // 1 买、2 卖
    state.last24h = {open, high, bottom, close, vol, rfvol, percent, direction}
  },
  addEvents (state, ev) {
    if (state.events[ev.name]) {
      state.events[ev.name].push(ev.fun)
    } else {
      state.events[ev.name] = [ev.fun]
    }
  },
  removeEvents (state, name) {
    delete state.events[name]
  },
  tiggerEvents (state, obj) {
    if (state.events[obj.name]) {
      state.events[obj.name].forEach((callback) => {
        callback(obj.params)
      })
    }
  },
}

const actions = {
  // get user wallet
  getMarkets({dispatch, commit, state }) {
    return new Promise(resolve => {
      getMarket().then(res => {
        let _markets = {}
        if(res.rst == SUCCESS){
          let config = {}
          res.data.forEach(item=>{
            config[item.market] = {
                minAmount: item.minAmount,
                minQuantity: item.minQuantity
            }
          })
          res.data.sort((m1, m2)=>{
            return (m2.idx||0) > (m1.idx||0) ? 1 : -1
          })
          commit('updateMarketConfig', config)
          commit('updateMarkets', res.data)
        }
        resolve(res)
      })
    })
  },
  addEvents (context, ev) {
    context.commit('addEvents', ev)
  },
  removeEvents (context, name) {
    context.commit('removeEvents', name)
  },
  tiggerEvents (context, obj) {
    context.commit('tiggerEvents', obj)
  },
}

export default {
  namespaced: true,
  getters,
  state,
  mutations,
  actions
}
